package cn.edu.hunau.service.impl;

import cn.edu.hunau.dao.ProductDao;
import cn.edu.hunau.dao.impl.ProductDaoImpl;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.pojo.PageResult;
import cn.edu.hunau.pojo.Product;
import cn.edu.hunau.pojo.ProductResult;
import cn.edu.hunau.service.ProductService;
import cn.edu.hunau.utils.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 苏佳
 * @Date 2022 12 11 23 08
 **/
public class ProductServiceImpl implements ProductService {
    private ProductDao productDao=new ProductDaoImpl();
    @Override
    public ProductResult<Map<String, Object>> loadProductByPid(Integer pid) {
        ProductResult<Map<String, Object>> productR = new ProductResult<>();
        String[] str = productDao.queryByPid(pid);
        //1 加载path数据
        List<Map<String, Object>> list0 = ProductResult.loadDataStringToList2(str[0]);
        productR.setPath(list0);

        //2 加载imagessrc数据
        List<Map<String, Object>> list1 = ProductResult.loadDataStringToList2(str[1]);
        productR.setImagessrc(list1);

        //3 加载goodsItem数据
        Product p = productDao.queryById(pid);
        Map<String, Object> map = new HashMap<>();
        map.put("title",p.getName());
        map.put("price",p.getPrice());
        Map<String, Object> promoteSales = JsonUtils.stringToMap(p.getPromoteSales(), String.class, Object.class);
        map.put("promoteSales",promoteSales);
        map.put("support",p.getSupport());
        map.put("address",p.getAddress());
        map.put("evaluateNum",p.getEvaluateNum());
        List<Map<String, Object>> list2 = ProductResult.loadDataStringToList2(str[2]);
        map.put("crumbData",list2);
        productR.setGoodsItem(map);

        //4 加载goodsDetails数据
        List<Map<String, Object>> list3 = ProductResult.loadDataStringToList2(str[3]);
        productR.setGoodsDetails(list3);
        return productR;
    }

    @Override
    public PageResult<Product> pageQueryByCid(int cid, int pageIndex, int pageSize) throws DaoException {
        PageResult<Product> pageResult = new PageResult<>();
        pageResult.setPageIndex(pageIndex);
        pageResult.setPageSize(pageSize);
        //总记录数
        int total = productDao.countById(cid);
        pageResult.setTotal(total);

        //总页数
        int pageTotal = total%pageSize == 0?total/pageSize:total/pageSize+1;

        pageResult.setPageTotal(pageTotal);

        //数据
        int beginIndex=(pageIndex-1)*pageSize;
        List<Product> data = productDao.queryByCidLimit(cid, beginIndex, pageSize);
        pageResult.setData(data);
        return pageResult;
    }

    @Override
    public PageResult<Product> pageQueryByKeyWord(String keyWord, int pageIndex, int pageSize) throws DaoException {
        PageResult<Product> pageResult = new PageResult<>();
        pageResult.setPageIndex(pageIndex);
        pageResult.setPageSize(pageSize);

        //总记录数
        int total = productDao.CountByKeyWord(keyWord);
        pageResult.setTotal(total);

        //总页数
        int pageTotal = total%pageSize == 0?total/pageSize:total/pageSize+1;

        pageResult.setPageTotal(pageTotal);

        //数据
        int beginIndex=(pageIndex-1)*pageSize;
        List<Product> data = productDao.queryByKeyWordLimit(keyWord, beginIndex, pageSize);
        pageResult.setData(data);
        return pageResult;
    }

    @Override
    public Product findProductById(int id) {
        return productDao.queryById(id);
    }
}
